﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Text;

namespace XmlToPdf
{
    public class Secret
    {
        // UTF-8编码声明
        private const string UTF8_ENCODING = "UTF-8";
        // 加密密钥
        private const string AES_KEY = "1234567890xayr--";
        // 加密用IV向量
        private const string AES_IV = "1234567890xayr==";

        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="encrptedData"></param>
        /// <returns></returns>
        public string decrptString(byte[] encrptedData)
        {
            // aes密钥长度必须为16位
            byte[] keyBytes = Encoding.GetEncoding(UTF8_ENCODING).GetBytes(AES_KEY);
            byte[] ivBytes = Encoding.GetEncoding(UTF8_ENCODING).GetBytes(AES_IV);

            // 解密字符串
            AesEncryptor aesEncpt = new AesEncryptor(keyBytes, ivBytes);
            byte[] decrptBytes = aesEncpt.DecryptString(encrptedData);
            string decrptedData = Encoding.GetEncoding(UTF8_ENCODING).GetString(decrptBytes, 0, decrptBytes.GetLength(0));
            return decrptedData;
        }

        // TODO 需要找到一种隔离开数据层的方法
        private const string XML_NODE_SALARY = "salary";
        private const string XML_NODE_NAME = "name";
        private const string XML_NODE_OVERTIME = "overTime";
        private const string XML_NODE_TEX = "tex";
        private const string XML_NODE_HOUSE = "house";
        private const string XML_NODE_OLD = "old";
        private const string XML_NODE_TOTAL = "total";

        /// <summary>
        /// 解析xml字符串，得到数据对象
        /// </summary>
        /// <param name="encrptedData">xml字符串</param>
        /// <returns>数据对象</returns>
        public static List<Salary> getXmlSalariesData(string xmlStr)
        {
            // xml文件内容解析
            XDocument xdoc = XDocument.Parse(xmlStr);
            var xmlSalaries = from salary in xdoc.Descendants(XML_NODE_SALARY)
                              where salary.Element(XML_NODE_NAME) != null
                              select new Salary
                              {
                                  name = (string)salary.Element(XML_NODE_NAME),
                                  overTime = (int)salary.Element(XML_NODE_OVERTIME),
                                  tex = (int)salary.Element(XML_NODE_TEX),
                                  house = (int)salary.Element(XML_NODE_HOUSE),
                                  old = (int)salary.Element(XML_NODE_OLD),
                                  total = (int)salary.Element(XML_NODE_TOTAL),                                  

                              };
            return xmlSalaries.ToList<Salary>();
        }

        
    }
}
